Conditions | 13 |
Total Lines | 59 |
Code Lines | 30 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like jquery.hotkeys.js ➔ keyHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /* |
||
35 | function keyHandler( handleObj ) { |
||
36 | // Only care when a possible input has been specified |
||
37 | if ( typeof handleObj.data !== "string" ) { |
||
38 | return; |
||
39 | } |
||
40 | |||
41 | var origHandler = handleObj.handler, |
||
42 | keys = handleObj.data.toLowerCase().split(" "); |
||
43 | |||
44 | handleObj.handler = function( event ) { |
||
45 | // Don't fire in text-accepting inputs that we didn't directly bind to |
||
46 | if ( this !== event.target && (/textarea|select/i.test( event.target.nodeName ) || |
||
47 | event.target.type === "text") ) { |
||
48 | return; |
||
|
|||
49 | } |
||
50 | |||
51 | // Keypress represents characters, not special keys |
||
52 | var special = event.type !== "keypress" && jQuery.hotkeys.specialKeys[ event.which ], |
||
53 | character = String.fromCharCode( event.which ).toLowerCase(), |
||
54 | key, modif = "", possible = {}; |
||
55 | |||
56 | // check combinations (alt|ctrl|shift+anything) |
||
57 | if ( event.altKey && special !== "alt" ) { |
||
58 | modif += "alt+"; |
||
59 | } |
||
60 | |||
61 | if ( event.ctrlKey && special !== "ctrl" ) { |
||
62 | modif += "ctrl+"; |
||
63 | } |
||
64 | |||
65 | // TODO: Need to make sure this works consistently across platforms |
||
66 | if ( event.metaKey && !event.ctrlKey && special !== "meta" ) { |
||
67 | modif += "meta+"; |
||
68 | } |
||
69 | |||
70 | if ( event.shiftKey && special !== "shift" ) { |
||
71 | modif += "shift+"; |
||
72 | } |
||
73 | |||
74 | if ( special ) { |
||
75 | possible[ modif + special ] = true; |
||
76 | |||
77 | } else { |
||
78 | possible[ modif + character ] = true; |
||
79 | possible[ modif + jQuery.hotkeys.shiftNums[ character ] ] = true; |
||
80 | |||
81 | // "$" can be triggered as "Shift+4" or "Shift+$" or just "$" |
||
82 | if ( modif === "shift+" ) { |
||
83 | possible[ jQuery.hotkeys.shiftNums[ character ] ] = true; |
||
84 | } |
||
85 | } |
||
86 | |||
87 | for ( var i = 0, l = keys.length; i < l; i++ ) { |
||
88 | if ( possible[ keys[i] ] ) { |
||
89 | return origHandler.apply( this, arguments ); |
||
90 | } |
||
91 | } |
||
92 | }; |
||
93 | } |
||
94 | |||
100 |